<template>
  <div>
    <el-container>
      <el-main>
        <el-card class="box-card" style="margin-left: -10px;margin-top:5px;width: 1040px;height: 70px">
          <div slot="header" class="clearfix" style="height: 30px;">
            <span style="float:left;margin-top: -10px;height: 30px">我的收藏（{{this.total}}）</span>
          </div>
        </el-card>

        <el-card class="box-card" v-for="t in collectTopicList" :key="index" style="margin-left: -10px;margin-top: -1px;width: 1040px;height: 120px">
          <div slot="" class="clearfix" style="height: 80px;">
<!--              <span style="float:left;margin-top: -70px;height: 30px;">-->
                <el-link target="_blank" @click="showTopicBtn(t.topicId)" style="font-size: 16px;float: left;font-weight:bold;">{{ t.content }}</el-link>
<!--              </span>-->
          </div>
          <div style="float:left;margin-top: -30px">
            <i class="el-icon-star-on" @click="collectAndCancel(t.id)"><span style="font-size: 12px">{{t.collectNum}}</span></i>
            <i class="el-icon-chat-dot-square" style="margin-left: 30px"><span style="font-size: 12px">{{t.commentNum}}</span></i>
          </div>
        </el-card>
        <!--分页组件-->
        <div class="block" v-show="showPagination" style="float:left;margin-left: 200px">
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNo"
              :page-sizes="[3, 5]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
          </el-pagination>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "userCenter-myCollect",
  data(){
    return{
      collectTopicList:[{
        collectNum: '',
        commentNum: '',
        content: '',
        id: '',
        time: '',
        topicId: ''
      }],
      pageNo: 1, //当前页数
      pageSize: 3, //显示条数
      total: 0, //总条数
      showPagination: false, //分页栏是否显示
    }
  },
  methods:{
    handleSizeChange(val) {
      this.pageSize=val;
      this.pageNo=1;//当切换一页显示几个的时候，我们要把页码切换第一页
      this.getCollectTopicList();
      //console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.pageNo=val;
      this.getCollectTopicList();
      //console.log(`当前页: ${val}`);
    },
    //加载收藏的题目列表
    getCollectTopicList(){
      this.$axios.post("/api/userCenter/getUserCollectTopicListJ",{
        pageNo:(this.pageNo - 1) * this.pageSize,
        pageSize:this.pageSize
      }).then(resp=>{
        console.log(resp)
        this.total=resp.data.data.total;
        this.collectTopicList=resp.data.data.list;
        if(this.collectTopicList.length!=0){
          this.showPagination=true;
        }
      })
    },
    //收藏与取消收藏
    collectAndCancel(id){ //id是收藏题目表的id
      //alert(id);
      this.$confirm('是否取消收藏?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.post("/api/userCenter/cancelTopicJ",{
          id:id
        }).then(resp=>{
          console.log(resp);
          this.getCollectTopicList();//再次获取收藏题列表，用来刷新页面
        })
        this.$message({
          type: 'success',
          message: '取消收藏成功!'
        });
        location.reload();//刷新页面
      })/*.catch(() => {
        this.$message({
          type: 'info',
          message: ''
        });
      });*/
    },
    //点击题目触发
    showTopicBtn(id){
      //跳转页面到详细题目页面，将题目id传过去
      let routeData = this.$router.resolve({
        path: "/showTopic",
        query: {
          id:id
        }
      });
      //必要操作，否则不会打开新页面
      window.open(routeData.href, '_blank');
    }
  },
  created() {
    //加载收藏的题目列表
    this.getCollectTopicList()
  }
}
</script>

<style scoped>
.el-main {
  background-color: #f1f2f6;
  color: #333;
  text-align: center;
  line-height: 40px;
}
</style>